import re

from django import forms

from apps.operations.models import UserAsk, UserFavorite

"""
用户咨询课程
"""
class UserAskForm(forms.ModelForm):
    # 关联UserAsk类,校验字段如下:
    class Meta:
        model = UserAsk
        fields = ["name","mobile","courseName"]

    # 校验用户名
    def clean_name(self):
        name = self.cleaned_data["name"]
        p = re.compile("^[\u4E00-\u9FFF_a-zA-Z]{2,15}$")
        if p.match(name):
            return name
        raise forms.ValidationError(u"用户名输入有误!",code="name_invalid")

    # 校验手机号码
    def clean_mobile(self):
        moblie = self.cleaned_data["mobile"]
        p = re.compile("^1[3|4|5|7|8]\d{9}$")
        if p.match(moblie):
            return moblie
        raise forms.ValidationError(u"手机号码输入有误!", code="mobile_invalid")

    # 校验课程名称
    def clean_courseName(self):
        courseName = self.cleaned_data["courseName"]
        p = re.compile("^[\u4E00-\u9FFF_a-zA-Z0-9]{2,20}$")
        if p.match(courseName):
            return courseName
        raise forms.ValidationError(u"课程名称输入有误!",code="course_name_invalid")

"""
用户收藏(机构,教师,课程)
"""
# class UserFavoriteForm(forms.ModelForm):
#     class Meta:
#         model = UserFavorite
#         fields = ["favId","favType"]
#
#     def _clean_(self):